package com.atguigu.exer2;

import org.junit.Test;

import java.io.File;
import java.io.FilenameFilter;

/**
 * @author yihu
 * @create 2021-03-09 14:32
 * @email: 1057202547@qq.com
 * @dec: 判断指定目录下是否有后缀名为.jpg的文件，如果有，就输出该文件名称
 */
public class FindJPGFileTest {
    @Test
    public  void test(){
        File file = new File("C:\\Users\\yihu\\Pictures\\Saved Pictures");
        String[] list = file.list();
        for (String filename:list){
            if(filename.endsWith(".jpg")){
                System.out.println(filename);
            }
        }
    }
    @Test
    public  void test2(){
        File file = new File("C:\\Users\\yihu\\Pictures\\Saved Pictures");
        File[] files = file.listFiles();
        for (File elmFile :files){
            boolean b = elmFile.getName().endsWith(".jpg");
            if(b){
                System.out.println(elmFile.getName());
            }
        }
    }
    /**
     * 	/*
     * 	 * File类提供了两个文件过滤器方法
     * 	 * public String[] list(FilenameFilter filter)
     * 	 * public File[] listFiles(FileFilter filter)
     *
     * */
    @Test
    public  void test3(){
        File file = new File("C:\\Users\\yihu\\Pictures\\Saved Pictures");
        //过滤出所有名称以。jpg结尾的文件
        String[] list = file.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".jpg");
            }
        });
        for (String filename:list){
            System.out.println(filename);
        }
    }
}
